//
//  MainVideoInfoView.m
//  HorizonLight
//
//  Created by BaQi on 15/9/23.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "MainVideoInfoView.h"

@implementation MainVideoInfoView

- (instancetype)initWithFrame:(CGRect)frame taget:(id)taget action:(SEL)action  section:(NSInteger)section row:(NSInteger)row array:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //创建一个scrollView
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 5 * 2)];
        //设置允许滑动
        self.scrollView.scrollEnabled = YES;
        self.scrollView.pagingEnabled = YES;
        //左右滑动
        self.scrollView.contentSize = CGSizeMake(kScreenWidth * ([array[section] count] + 2), 0);
        //边界回弹
        self.scrollView.bounces = YES;
        //时候显示水平指示器
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.contentOffset = CGPointMake(kScreenWidth * (row + 1), 0);
        for (int i = 0; i < [array[section] count] + 2; i++)
        {
            self.settingImageView = [[TouchImageView alloc]initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, _scrollView.height) taget:taget action:action];
            self.settingImageView.backgroundColor = [UIColor redColor];
            self.settingImageView.tag = 1000 + i;
            [self.scrollView addSubview:self.settingImageView];
        }
        //播放按钮的图片
        self.playImageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - kPlayImgViewWidth) / 2, (_scrollView.height - kPlayImgViewheight) / 2, kPlayImgViewWidth, kPlayImgViewheight)];
        self.playImageView.image = [UIImage imageNamed:@"player_play@2x"];
        
        //内容背景
        self.contentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, _scrollView.bottom, kScreenWidth, self.height - _scrollView.height)];
        //打开背景图片的交互
        self.contentImageView.userInteractionEnabled = YES;
        self.equalH = _contentImageView.height / 10;
        
        //标题
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kEdge, 0, _contentImageView.width - 2 * kEdge, self.equalH)];
        
        //分类和时间
        self.classLabel = [[UILabel alloc]initWithFrame:CGRectMake(kEdge, _titleLabel.bottom, kScreenWidth - 2 * kEdge, self.equalH / 3 * 2)];
        
        //内容简介
        self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(kEdge, _classLabel.bottom + self.equalH, kScreenWidth - 2 * kEdge, 3.5 * self.equalH)];
        
        //加入收藏按钮
        self.clickPraiseButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.clickPraiseButton.frame = CGRectMake(kEdge * 4, _contentImageView.height - 60, kIconWidth , kIconHeight);
        [self.clickPraiseButton setBackgroundImage:[UIImage imageNamed:@"FontAwesome_f08a(0)_48"] forState:(UIControlStateNormal)];
        [self.clickPraiseButton setBackgroundImage:[UIImage imageNamed:@"FontAwesome_f004(0)_48"] forState:(UIControlStateSelected)];
        //收藏次数
        self.clickPraiseLabel = [[UILabel alloc]initWithFrame:CGRectMake(kEdge * 4 + kIconWidth, _clickPraiseButton.top, (kScreenWidth - _clickPraiseButton.right), kLabelHeight)];
        
        //分享按钮
        self.shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.shareButton.frame = CGRectMake(kScreenWidth / 2 + kEdge * 3, _clickPraiseButton.top, kIconWidth, kIconHeight);
        [self.shareButton setBackgroundImage:[UIImage imageNamed:@"FontAwesome_f0ac(0)_48"] forState:(UIControlStateNormal)];
        //分享次数
        self.shareLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth / 2 + kIconWidth + kEdge * 3, _clickPraiseButton.top, _clickPraiseLabel.width, kLabelHeight)];
        
        //分割线
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(15, 45, [self.titleLabel.text length] * 13, 1)];
        view.backgroundColor = [UIColor whiteColor];
        view.tag = 2000;
        [self.contentImageView addSubview:view];
        
        
        
        //label的方法调用
        [self setUpTitleLabel:self.titleLabel];
        [self setUpLabel:self.classLabel];
        [self setUpLabel:self.contentLabel];
        [self setUpLabel:self.clickPraiseLabel];
        [self setUpLabel:self.shareLabel];
        [self.contentImageView addSubview:self.clickPraiseButton];
        [self.contentImageView addSubview:self.shareButton];
        [self addSubview:self.scrollView];
        [self addSubview:self.playImageView];
        [self addSubview:self.contentImageView];
    }
    return self;
}

#pragma mark ------Label的属性
- (void)setUpLabel:(UILabel *)label
{
    //颜色
    label.textColor = [UIColor whiteColor];
    //居左
    label.textAlignment = NSTextAlignmentLeft;
    //字体和大小
    label.font = [UIFont systemFontOfSize:13];
    //全部显示
    label.numberOfLines = 0;
    [self.contentImageView addSubview:label];
}
#pragma mark ------标题Laebl
- (void)setUpTitleLabel:(UILabel *)label
{
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont boldSystemFontOfSize:17];
    label.numberOfLines = 0;
    [self.contentImageView addSubview:label];
}
#pragma mark ---------------  未知
- (void)setUpViewWithModel:(VideoListModel*)model section:(NSInteger)section array:(NSMutableArray *)array
{
    for (int i = 0; i <= [array[section] count] + 1; i++)
    {
        TouchImageView *touchImgView = (TouchImageView *)[self viewWithTag:1000 + i];
        if (i == [array[section] count] + 1 || i == 0)
        {
            touchImgView.image = [UIImage imageNamed:@"yun.gif"];
        }
        else
        {
            VideoListModel *model = array[section][i - 1];
            [touchImgView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        }
    }
    
    [self changeContentValueWithModel:model];
}


#pragma mark ---------------  给各个控件赋值
- (void)changeContentValueWithModel:(VideoListModel *)model
{
    NSMutableArray* mArray = [[NewsDataBase shareDataBase] allNewsOfDBTable:@"myCollectionDB"];
    if ([mArray count] > 0)
    {
        for (VideoListModel* m in mArray)
        {
            if ([m.title isEqualToString:model.title])
            {
                self.clickPraiseButton.selected = YES;
            }
            else
            {
                self.clickPraiseButton.selected = NO;
            }
            
        }
        
    }
    
    [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:model.coverBlurred] placeholderImage:[UIImage imageNamed:@"yun.gif"]];
    //标题
    self.titleLabel.text = model.title;
    
    //分割线
    UIView *view = (UIView *)[self viewWithTag:2000];
    view.frame = CGRectMake(kEdge, kScreenHeight / 20, [self.titleLabel.text length] * 13, 1);
    
    //类别和时间
    self.classLabel.text = [NSString stringWithFormat:@"#%@ / %02ld'%02ld\"", model.category, [model.duration integerValue] / 60, [model.duration integerValue] % 60];
    
    //内容简介
    self.contentLabel.text = model.videoListDescription;
    CGRect frame = [self.contentLabel.text boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kEdge, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    self.contentLabel.frame = CGRectMake(kEdge, _classLabel.bottom + self.equalH / 2, kScreenWidth - 2 * kEdge, frame.size.height);
    
    //收藏数
    _clickPraiseLabel.text = [NSString stringWithFormat:@"%ld", model.collectionCount];
    
    //分享
    _shareLabel.text = [NSString stringWithFormat:@"%ld", model.shareCount];
}


@end
